<?php
	/* ******************************************************** */
	/* Arcans Project (2010)                                    */
	/* Jeu 3D en temps réel par navigateur, conception et       */
	/* Développement dans la notice                             */
	/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
	/* ******************************************************** */

	//*****************************
	//classe de création de la page
	//*****************************
	class PageClass {
		//propriétés :
		private $text;
		//méthodes :
		function __construct($objXml) { $this->text = $objXml; }
		public function affichage() {
			echo '<div id="page">';
			$this->switchAff($this->text);
			echo '</div>';
		}
		public function affichageXML($doc, $body) {
			$div = $doc->createElement('div');
			$div->setAttribute('id','page');
			//echo '<div id="page">';

			$this->switchAffXML($this->text, $doc, $div, $body);
			//$this->switchAff($this->text);

			$body->appendChild($div);
			//echo '</div>';
		}
		private function switchAff($tabstruct) {
			foreach ($tabstruct as $elt) {
				switch ($elt["type"]) {
					case "table" :
						echo '<table'; if ($elt["data"][0] != "") echo ' id="'.$elt["data"][0].'" '; echo '>';
						$this->switchAff($elt["data"][1]); echo '</table>'; break;
					case "contain" :
						echo '<div'; if ($elt["data"][0] != "") echo ' id="'.$elt["data"][0].'" '; echo '>';
						$this->switchAff($elt["data"][1]); echo "</div>"; break;
					case "row" :
						echo '<tr'; if ($elt["data"][0]) echo ' id="'.$elt["data"][0].'" '; echo '>';
						$this->switchAff($elt["data"][1]); echo '</tr>'; break;
					case "cellf" :
						echo '<td'; if ($elt["data"][0] != "") echo ' colspan='.$elt["data"][0].' ';
						if ($elt["data"][1] != "") echo ' id="'.$elt["data"][1].'" '; echo '>';
						$this->switchAff($elt["data"][2]); echo '</td>'; break;
					case "liste" :
						echo '<ul'; if ($elt["data"][0] != "") echo ' style="'.$elt["data"][0].'"';
						if ($elt["data"][1] != "") echo ' id="'.$elt["data"][1].'"';
						echo '>'; $this->switchAff($elt["data"][2]); echo "</ul>"; break;
					case "eltlst" :
						echo '<li'; if ($elt["data"][0] != "") echo ' style="'.$elt["data"][0].'"';
						echo '>'.$elt["data"][1].'</li>'; break;
					case "celle" : echo '<td>'.$elt["data"].'</td>'; break;
					case "titre1" : echo '<h1>'.$elt["data"].'</h1>'; break;
					case "titre2" : echo '<h2>'.$elt["data"].'</h2>'; break;
					case "titre3" : echo '<hr width="65%" /><h4>'.$elt["data"].'</h4>'; break;
					case "div" : echo '<div'; if ($elt["data"] != "") echo ' id="'.$elt["data"].'" '; echo '></div>'; break;
					case "para" : echo '<p class="para">'.$elt["data"].'</p>'; break;
					case "textarea" :
						echo '<textarea class="textarea" id="'.$elt["data"][0].'"';
						if ($elt["data"][1] != "") echo ' onclick="'.$elt["data"][1].'"';
						echo '></textarea>'; break;
					case "label" : echo '<label for="'.$elt["data"][0].'">'.$elt["data"][1].'<label>'; break;
					case "link" : echo '<a href="'.$elt["data"][0].'">'.$elt["data"][1].'</a>'; break;
					case "erreur" : echo '<p class="erreur">'.$elt["data"].'</p>'; break;
					case "text" : echo $elt["data"]; break;
					case "input" :
						echo '<input type="'.$elt["data"][0].'" id="'.$elt["data"][1].'"';
						if ($elt["data"][2] != "") echo ' value="'.$elt["data"][2].'"';
						if ($elt["data"][3] != "") echo ' onclick="javascript:'.$elt["data"][3].'"';
						echo ' />'; break;
					case "button" :
						echo '<button '; if ($elt["data"][0] != "") echo 'id="'.$elt["data"][0].'" ';
						echo 'onclick="javascript:'.$elt["data"][1].'">'.$elt["data"][2].'</button>'; break;
					case "champs" :
						$mysqlchamps = new MysqlClass(0);
						$res = $mysqlchamps->resultFun("select", "users", "users='".$_SESSION["pseudo"]."'");
						$res = $mysqlchamps->resultFun("select", $elt["data"][0], "id=".$res["id"]);
						$info_echo = $res[$elt["data"][1]]; echo '<td id="'.$elt["data"][1].'">'.$info_echo.'</td>'; break;
					case "connect" :
						if (isset($_SESSION["level"])) { if ($elt["data"][0] == 1) $this->switchAff($elt["data"][1]); }
						elseif ($elt["data"][0] == 0) $this->switchAff($elt["data"][1]);
						break;
				}
			}
		}
		private function switchAffXML($tabstruct, $doc, $div, $body) {
			if (is_array($tabstruct)) {
				foreach ($tabstruct as $elt) {
					switch ($elt["type"]) {
						case "table" :
							$table = $doc->createElement('table');
							//echo '<table';
							if ($elt["data"][0] != "") {
								$table->setAttribute('id',$elt["data"][0]);
								//echo ' id="'.$elt["data"][0].'" ';
								//echo '>';
							}
							$this->switchAffXML($elt["data"][1], $doc, $div);
							$body->appendChild($table);
							//echo '</table>';
							break;
						case "contain" :
							$div = $doc->createElement('div');
							//echo '<div';
							if ($elt["data"][0] != "") {
								$div->setAttribute('id',$elt["data"][0]);
								//echo ' id="'.$elt["data"][0].'" ';
								//echo '>';
							}
							$this->switchAffXML($elt["data"][1], $doc, $div);
							$body->appendChild($div);
							//echo "</div>";
							break;
						case "row" :
							$tr = $doc->createElement('tr');
							//echo '<tr';
							if ($elt["data"][0]) {
								$tr->setAttribute('id',$elt["data"][0]);
								//echo ' id="'.$elt["data"][0].'" ';
								//echo '>';
							}
							$this->switchAffXML($elt["data"][1], $doc, $div);
							$table->appendChild($tr);
							//echo '</tr>';
							break;
						case "cellf" :
							$td = $doc->createElement('td');
							//echo '<td';
							if ($elt["data"][0] != "") {
								$td->setAttribute('colspan',$elt["data"][0]);
								//echo ' colspan='.$elt["data"][0].' ';
							}
							if ($elt["data"][1] != "") {
								$td->setAttribute('id',$elt["data"][1]);
								//echo ' id="'.$elt["data"][1].'" ';
								//echo '>';
							}
							$this->switchAffXML($elt["data"][2], $doc, $div);
							$td->appendChild($tr);
							//echo '</td>';
							break;
						case "liste" :
							$ul = $doc->createElement('ul');
							//echo '<ul';
							if ($elt["data"][0] != "") {
								$ul->setAttribute('style',$elt["data"][0]);
								//echo ' style="'.$elt["data"][0].'"';
							}
							if ($elt["data"][1] != "") {
								$ul->setAttribute('id',$elt["data"][1]);
								//echo ' id="'.$elt["data"][1].'"';
							}
							//echo '>';
							$this->switchAffXML($elt["data"][2], $doc, $div);
							$td->appendChild($ul);
							//echo "</ul>";
							break;
						case "eltlst" :
							$li = $doc->createElement('li');
							//echo '<li';
							if ($elt["data"][0] != "") {
								$li->setAttribute('style',$elt["data"][0]);
								//echo ' style="'.$elt["data"][0].'"';
							}
							$li->appendChild($doc->createTextNode($elt["data"][1]));
							//echo '>'.$elt["data"][1].'</li>';
							$ul->appendChild($li);
							break;
						case "celle" :
							$td = $doc->createElement('td');
							$td->appendChild($doc->createTextNode($elt["data"]));
							$tr->appendChild($td);
							//echo '<td>'.$elt["data"].'</td>';
							break;
						case "titre1" :
							$h1 = $doc->createElement('h1');
							$h1->appendChild($doc->createTextNode($elt["data"]));
							$div->appendChild($h1);
							//echo '<h1>'.$elt["data"].'</h1>';
							break;
						case "titre2" :
							$h2 = $doc->createElement('h2');
							$h2->appendChild($doc->createTextNode($elt["data"]));
							$div->appendChild($h2);
							//echo '<h2>'.$elt["data"].'</h2>';
							break;
						case "titre3" :
							$hr = $doc->createElement('hr');
							$hr->setAttribute('width','65%');
							$div->appendChild($hr);
							$h3 = $doc->createElement('h4');
							$h3->appendChild($doc->createTextNode($elt["data"]));
							$div->appendChild($h3);
							//echo '<hr width="65%" /><h4>'.$elt["data"].'</h4>';
							break;
						case "div" :
							$div = $doc->createElement('div');
							//echo '<div';
							if ($elt["data"] != "") {
								$div->setAttribute('id',$elt["data"]);
								//echo ' id="'.$elt["data"].'" ';
							}
							$body->appendChild($div);
							//echo '></div>';
							break;
						case "para" :
							$p = $doc->createElement('p');
							$p->setAttribute('class','para');
							$p->appendChild($doc->createTextNode($elt["data"]));
							$div->appendChild($p);
							//echo '<p class="para">'.$elt["data"].'</p>';
							break;
						case "textarea" :
							$textarea = $doc->createElement('textarea');
							$textarea->setAttribute('class','textarea');
							$textarea->setAttribute('id',$elt["data"][0]);

							//echo '<textarea class="textarea" id="'.$elt["data"][0].'"';
							if ($elt["data"][1] != "") {
								$textarea->setAttribute('onclick',$elt["data"][1]);
								//echo ' onclick="'.$elt["data"][1].'"';
							}
							$td->appendChild($textarea);
							//echo '></textarea>';
							break;
						case "label" :
							$label = $doc->createElement('label');
							$label->setAttribute('for',$elt["data"][0]);
							$label->appendChild($doc->createTextNode($elt["data"][1]));
							//echo '<label for="'.$elt["data"][0].'">'.$elt["data"][1].'<label>';
							$td->appendChild($label);
							break;
						case "link" :
							$a = $doc->createElement('a');
							$a->setAttribute('href',$elt["data"][0]);
							$a->appendChild($doc->createTextNode($elt["data"][1]));
							$td->appendChild($a);
							//echo '<a href="'.$elt["data"][0].'">'.$elt["data"][1].'</a>';
							break;
						case "erreur" :
							$p = $doc->createElement('p');
							$p->setAttribute('class','erreur');
							$p->appendChild($doc->createTextNode($elt["data"]));
							$div->appendChild($p);
							//echo '<p class="erreur">'.$elt["data"].'</p>';
							break;
						case "text" :
							$td->appendChild($doc->createTextNode($elt["data"]));
							//echo $elt["data"];
							break;
						case "input" :
							$input = $doc->createElement('input');
							$input->setAttribute('type',$elt["data"][0]);
							$input->setAttribute('id',$elt["data"][1]);
							$input->appendChild($doc->createTextNode($elt["data"][1]));
							//echo '<input type="'.$elt["data"][0].'" id="'.$elt["data"][1].'"';
							if ($elt["data"][2] != "") {
								$input->setAttribute('value',$elt["data"][2]);
								//echo ' value="'.$elt["data"][2].'"';
							}
							if ($elt["data"][3] != "") {
								$input->setAttribute('onclick','javascript:'.$elt["data"][3]);
								//echo ' onclick="javascript:'.$elt["data"][3].'"';
							}
							$td->appendChild($input);
							//echo ' />';
							break;
						case "button" :
							$button = $doc->createElement('button');
							//echo '<button ';
							if ($elt["data"][0] != "") {
								$button->setAttribute('id',$elt["data"][0]);
								//echo 'id="'.$elt["data"][0].'" ';
							}
							$button->setAttribute('onclick','javascript:'.$elt["data"][1]);
							$button->appendChild($doc->createTextNode($elt["data"][2]));
							//echo 'onclick="javascript:'.$elt["data"][1].'">'.$elt["data"][2].'</button>';
							$td->appendChild($button);
							break;
						case "champs" :
							$mysqlchamps = new MysqlClass(0);
							$res = $mysqlchamps->resultFun("select", "users", "users='".$_SESSION["pseudo"]."'");
							$res = $mysqlchamps->resultFun("select", $elt["data"][0], "id=".$res["id"]);
							$info_echo = $res[$elt["data"][1]];
							$td = $doc->createElement('td');
							$td->setAttribute('id',$elt["data"][1]);
							$td->appendChild($doc->createTextNode($info_echo));
							$tr->appendChild($td);
							//echo '<td id="'.$elt["data"][1].'">'.$info_echo.'</td>';
							break;
						case "connect" :
							if (isset($_SESSION["level"])) {
								if ($elt["data"][0] == 1) {
									$this->switchAffXML($elt["data"][1], $doc, $div);
								}
							}
							elseif ($elt["data"][0] == 0) {
								$this->switchAffXML($elt["data"][1], $doc, $div);
							}
							break;
					}
				}
			}
		}
	}
?>